package com.tao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tao.config.MyLog;
import com.tao.pojo.SysLog;
import com.tao.pojo.vo.ResponseResult;
import com.tao.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/syslog")
public class CmsSysLogController {
//
    @Autowired
    private SysLogService sysLogService;

    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('admin')")
    @MyLog(value = "日志列表")
    public Object list(@RequestBody SysLog sysLog){
        HashMap<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<SysLog> lqw = new LambdaQueryWrapper<>();
        Page<SysLog> page = new Page<>(sysLog.getCurrent(),sysLog.getSize());
        lqw.like(!ObjectUtils.isEmpty(sysLog.getUsername()),SysLog::getUsername,sysLog.getUsername());
        lqw.orderByDesc(SysLog::getCreateDate);
        Page<SysLog> list = sysLogService.page(page, lqw);

        map.put("list",list.getRecords());
        map.put("total",list.getTotal());
        return ResponseResult.success(map);
    }
}
